package cn.gson.springboot.controllers;



import java.io.IOException;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.UserDao;
import cn.gson.springboot.model.pojos.User;
import cn.gson.springboot.model.service.GrantService;
import cn.gson.springboot.model.service.TriageService;
import cn.gson.springboot.model.service.UserService;

@Controller
@SessionAttributes({"ids","user"})
public class MainController {
	//排班
	@Autowired
	private TriageService trs;

	@Autowired
	private GrantService gs;
	@Autowired
	private UserService us;

	
	@Autowired
	private UserDao ud;
	/**
	 * 首页
	 */
	 //登陆页面
		@RequestMapping("Land")
		public void denglu() {
		}
	
	 //登陆
		@PostMapping(value="Land")
		@ResponseBody
		public String land(Model l,String userName,String userPwd){			
			User user = ud.cxyh2(userName, userPwd);
			if(user != null){
				l.addAttribute("user",user);	
				return "0";	
			}else{					
				return "1";	
			}
			
		}
	
		//注销登陆
		@PostMapping("zhuxiao")
		@ResponseBody
		public void zhuxiao(HttpSession session ,SessionStatus sessionStatus){
			session.removeAttribute("user");
			session.invalidate();
			sessionStatus.setComplete();
		}

	@RequestMapping("hospitalMain")
	public void main(Model model,HttpSession session) {
		//一级
		model.addAttribute("fts", gs.allFirstFunction(session));	
		//这是2级		
		model.addAttribute("funs", gs.allFunctionByUser(session));		
		//用户登录时，查询该用户所有的功能列表，并且去重		
		User user= (User) session.getAttribute("user");							
		Set<Integer> set = gs.allFunIdByUser(user.getUserId());
		for (Integer integer : set) {
		        integer.intValue();   
		}
			model.addAttribute("ids", gs.allFunIdByUser(user.getUserId()));
		
	}
	
	 /*查询用户的所有三级功能*/
	@RequestMapping("init-thirdfuns")
	@ResponseBody
	public JSONArray initThirdfuns(@RequestParam("funId")int parentId,HttpSession session){
		User user = (User) session.getAttribute("user");							
		int userId=user.getUserId();
	
		return gs.allThirdFuns(userId, parentId);
	}
	
	 /*查询某用户下的角色和功能*/
		@RequestMapping("init-allfourFuns")
		@ResponseBody
		public JSONArray allfourFuns(@RequestParam("funId")int parentId,HttpSession session){
			User user = (User) session.getAttribute("user");						
			int userId=user.getUserId();
			return gs.allfourFuns(userId, parentId);
			
		}
}
